/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.service;

import com.factory.server.helper.InputWrapper;
import java.util.LinkedList;

/**
 * Queue for inputs. Inputs are popped and saved to DB.
 * @author milos
 */
public class InputQueue {

    private static LinkedList<InputWrapper> inputWrapperList;
    private boolean empty = true;

    public InputQueue() {
        inputWrapperList = new LinkedList<InputWrapper>();
    }

    public synchronized void put(InputWrapper input) {
        inputWrapperList.add(input);
        empty = false;
        notifyAll();

    }

    public synchronized InputWrapper get() {
        while (empty == true) {	//wait till something appears in the buffer
            try {
                wait();
            } catch (InterruptedException e) {
                //TODO
            }
        }
        if (inputWrapperList.isEmpty()) {
            empty = true;
        }
        if (inputWrapperList.size() > 0) {
            return inputWrapperList.pop();
        } else {
            return null;
        }
    }

    public synchronized boolean isEmpty() {
        return empty;
    }

    public synchronized void setInputBOList(LinkedList<InputWrapper> results) {
        this.inputWrapperList = results;
    }

    public LinkedList<InputWrapper> getInputBOList() {
        return this.inputWrapperList;
    }
}
